package com.core.shared.file;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-12-29
 * Time: 下午4:29
 * To change this template use File | Settings | File Templates.
 */
public class FTPHandle {

    private static FTPClient ftp = new FTPClient();
    private static String url = "192.168.0.103";
    private static int port = 21;
    private static String username = "lxf";
    private static String password = "123456";

    private static boolean connectFTP() {
        try {
            if (port > -1) {
                ftp.connect(url, port);
            } else {
                ftp.connect(url);
            }
            if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                if (ftp.login(username, password)) {
                    return true;
                }
            }
            disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static void disconnect() {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Description: 向FTP服务器上传文件
     *
     * @param path     FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input    输入流
     * @return 成功返回true，否则返回false
     */
    public static boolean uploadFile(String path, String filename, InputStream input) {
        boolean success = false;
        try {
            if (connectFTP()) {
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                ftp.changeWorkingDirectory(path);
                ftp.storeFile(filename, input);
                input.close();
                ftp.logout();
                success = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            disconnect();
        }
        return success;
    }


    /**
     * Description: 从FTP服务器下载文件
     *
     * @param remotePath FTP服务器上的相对路径
     * @param fileName   要下载的文件名
     * @param localPath  下载后保存到本地的路径
     * @return
     */
    public static boolean downloadFile(String remotePath, String fileName, String localPath) {
        boolean success = false;
        try {
            if (connectFTP()) {
                ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录
                FTPFile[] fs = ftp.listFiles();
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        File localFile = new File(localPath + "/" + ff.getName());
                        OutputStream is = new FileOutputStream(localFile);
                        ftp.retrieveFile(ff.getName(), is);
                        is.flush();
                        is.close();
                    }
                }
                ftp.logout();
                success = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            disconnect();
        }
        return success;
    }

    /**
     * 远程删除FTP上的文件
     *
     * @param remotePath 远程文件路径
     * @param fileName   待删除的文件名
     * @return
     */
    public static boolean deleteFtpFile(String remotePath, String fileName) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            if (connectFTP()) {
                ftp.changeWorkingDirectory(remotePath);  //转移到FTP服务器目录
                success = ftp.deleteFile(remotePath + "/" + fileName);
                ftp.logout();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            disconnect();
        }
        return success;
    }
}
